package app.view.helper
{
	import app.model.vo.AlertVO;
	import app.view.module.IndexGaiaPageModule;
	
	import com.gaiaframework.api.Gaia;
	import com.gaiaframework.api.IPageAsset;
	import com.gaiaframework.api.IStyleSheet;
	import com.gaiaframework.debug.GaiaDebug;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import app.view.event.AlertEvent;
	
	public class Alert extends MovieClip implements AlertInterface
	{
		public static const OK:String = "OK";
		public static const YES_NO:String = "YES_NO";
		public static const NONE:String = "NONE";
		//the spinner is an asset of the index page - we are using the alert system to display it
		public static const SPINNER:String = "SPINNER";
		
		
		private var indexPage:IPageAsset;
		private var titleTF:TextFormat;
		private var msgTF:TextFormat;
		private var btnLblTF:TextFormat
		private var myStage:MovieClip;
		public var titleField:TextField;
		public var messageField:TextField;
		
		private var ok:Action_Btn;
		private var yes:Action_Btn;
		private var no:Action_Btn;
			
		public function Alert()
		{
			GaiaDebug.log('Alert');
			alpha = 0;
			visible = false;		
			
		}
		
		public function init():void
		{
			if(indexPage == null)
			{
				config();
			}
		}
		
		private function config():void
		{
			indexPage = Gaia.api.getPage("index");
			if(indexPage)
			{
				titleTF = IStyleSheet(indexPage.assets.css).transformStyle(".alertTitleText");
				msgTF = IStyleSheet(indexPage.assets.css).transformStyle(".alertMessageText");
				btnLblTF = IStyleSheet(indexPage.assets.css).transformStyle(".alertButtonLabelText");
				
				myStage = new Alert_Box();
				titleField = myStage.getChildByName("title_txt") as TextField;
				messageField = myStage.getChildByName("msg_txt") as TextField;
				addChild(myStage);
			}
		}
		public function get self():Alert
		{
			return this;
		}
		public function show(alert:AlertVO):void
		{
			GaiaDebug.log('Alert.show');
			removeAll();
			init();
			if(indexPage && alert.type != SPINNER)
			{
				IndexGaiaPageModule(indexPage.root).formatText(titleField, alert.title, titleTF);
				IndexGaiaPageModule(indexPage.root).formatText(messageField, alert.message, msgTF);
				messageField.width = 208;
				messageField.multiline = true;
				messageField.wordWrap = true;
				setupOptions(alert.type);
				alpha = 1;
				visible = true;
			}
		}
		public function setupOptions(type:String):void
		{
			switch(type)
			{
				case OK:
					setupOK();
					break
				case YES_NO:
					setupYES_NO();
					break;
				case NONE:
					break;
				
			}
		}
		private function removeAll():void
		{
			if(ok)
			{
				ok.removeEventListener(MouseEvent.CLICK, onOK);
				myStage.removeChild(ok);
				ok = null;
			}
			if(yes)
			{
				yes.removeEventListener(MouseEvent.CLICK, onYES);
				myStage.removeChild(yes);
				yes = null;
			}
			if(no)
			{
				no.removeEventListener(MouseEvent.CLICK, onNO);
				myStage.removeChild(no);
				no = null;
			}

			
		}
		private function setupOK():void
		{
			ok = new Action_Btn();
			var lbl:TextField = ok.getChildByName("label_txt") as TextField;
			IndexGaiaPageModule(indexPage.root).formatText(lbl, "OK", btnLblTF, TextFieldAutoSize.CENTER);
			ok.x = (myStage.width - ok.width)/2;
			ok.y = myStage.height - ok.height - 18;
			ok.addEventListener(MouseEvent.CLICK, onOK);
			myStage.addChild(ok);
			
		}
		private function setupYES_NO():void
		{
			yes = new Action_Btn();
			var yeslbl:TextField = yes.getChildByName("label_txt") as TextField;
			IndexGaiaPageModule(indexPage.root).formatText(yeslbl, "YES", btnLblTF, TextFieldAutoSize.CENTER);
			yes.x = (myStage.width - (yes.width)*2)/3;
			yes.y = myStage.height - ok.height - 18;
			yes.addEventListener(MouseEvent.CLICK, onYES);
			myStage.addChild(yes);

			no = new Action_Btn();
			var nolbl:TextField = no.getChildByName("label_txt") as TextField;
			IndexGaiaPageModule(indexPage.root).formatText(nolbl, "NO", btnLblTF, TextFieldAutoSize.CENTER);
			no.x = (yes.x*2) + yes.width;
			no.y = myStage.height - yes.height - 18;
			no.addEventListener(MouseEvent.CLICK, onNO);
			myStage.addChild(no);
			
		}
		
		private function onOK(e:MouseEvent):void
		{
			dispatchEvent(new AlertEvent(AlertEvent.HIDE) );
		}
		private function onYES(e:MouseEvent):void
		{
			dispatchEvent(new AlertEvent(AlertEvent.HIDE) );
		}
		private function onNO(e:MouseEvent):void
		{
			dispatchEvent(new AlertEvent(AlertEvent.HIDE) );
		}
		

		
		public function hide():void
		{
			GaiaDebug.log('Alert.hide');
			alpha = 0;
			visible = false;
			removeAll();
		}
		
		
		
	}
}